package com.smart.milvus.base;

import com.alibaba.fastjson.JSONObject;
import com.smart.milvus.exception.MilvusException;

public interface JsonTypeHandler {

    default <T> T parse(JSONObject json, Class<T> targetClass) {
        if (!targetClass.isInstance(json)) {
            throw new MilvusException("JSON type has to be JSONObject！");
        }
        return targetClass.cast(json);
    }

    default <T> JSONObject toJson(T obj) {
        if (!(obj instanceof JSONObject)) {
            throw new MilvusException("JSON type has to be JSONObject！");
        }
        return (JSONObject) obj;
    }
}
